package leecode.stringAndArray.String;

import org.junit.Test;

public class longestCommonPrefix {
    @Test
    public void test() {
        String str = solution1(new String[]{"lee","leecode","leemeimei","leejie"});
        System.out.println(str);
    }

    public String solution1(String[] strs){
        if (strs==null || strs.length==0){
            return "";
        }

        int count = strs.length;
        String prefix = strs[0];
        for (int i = 0; i < count; i++) {
            prefix = longestCommonPrefix(prefix,strs[i]);
            if(prefix.length()==0){
                break;
            }
        }
        return prefix;

    }

    public String longestCommonPrefix(String str1,String str2){
        int length = Math.min(str1.length(),str2.length());
        int count=0;
        while(count<length && str1.charAt(count)==str2.charAt(count)){
            count++;
        }
        return str1.substring(0,count);
    }


}

class mysolution{

    public void solution(){
        String[] strings = new String[]{"ab", "a"};
        int pos=0;
        boolean flag = true;
        while(flag){
            for (int i = 0; i < strings.length; i++) {
                if(strings[i].length()==0 || pos==strings[i].length()){
                    flag =false;
                    break;
                }
                if(!(strings[0].charAt(pos) == strings[i].charAt(pos))){
                    flag = false;
                    break;
                }
            }
            if(flag)
                pos++;
            else
                break;
        }
        if(pos==0){
            System.out.println("我是空的");
        }else{
            System.out.println(strings[0].substring(0,pos));
        }
    }

}

class solution1{




}
